package com.lyc.controller;

import com.lyc.pojo.Result;
import com.lyc.service.GaodeMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/gaode")
public class GaodeMapController {

    @Autowired
    private GaodeMapService gaodeMapService;

    /**
     * 地理编码接口：将地址转换为经纬度
     *
     * @param address 地址
     * @return 经纬度信息
     */
    @GetMapping("/geocode")
    public Result<String> geocode(@RequestParam String address) {
        try {
            String result = gaodeMapService.geocode(address);
            System.out.println("Geocode result: " + result); // 添加调试日志
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常堆栈
            return Result.error("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }

    /**
     * 逆地理编码接口：将经纬度转换为地址
     *
     * @param location 经纬度 (格式: 经度,纬度)
     * @return 地址信息
     */
    @GetMapping("/regeocode")
    public Result<String> regeocode(@RequestParam String location) {
        try {
            String result = gaodeMapService.regeocode(location);
            System.out.println("Regeocode result: " + result); // 添加调试日志
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常堆栈
            return Result.error("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }

    /**
     * 路径规划接口：计算两点间的路线
     *
     * @param origin      起点坐标 (格式: 经度,纬度)
     * @param destination 终点坐标 (格式: 经度,纬度)
     * @return 路线信息
     */
    @GetMapping("/route")
    public Result<String> calculateRoute(@RequestParam String origin, @RequestParam String destination) {
        try {
            String result = gaodeMapService.calculateRoute(origin, destination);
            System.out.println("Route result: " + result); // 添加调试日志
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常堆栈
            return Result.error("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }

    /**
     * 周边搜索接口：搜索指定坐标附近的POI
     *
     * @param location 中心点坐标 (格式: 经度,纬度)
     * @param keywords 搜索关键词
     * @return POI信息
     */
    @GetMapping("/nearby")
    public Result<String> searchNearby(@RequestParam String location, @RequestParam String keywords) {
        try {
            String result = gaodeMapService.searchNearby(location, keywords);
            System.out.println("Nearby search result: " + result); // 添加调试日志
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常堆栈
            return Result.error("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }
}